package com.dizner.androidcarclient;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

public class SuperDiverButton extends View {

    private static final String TAG = "SuperDiverButton";


    /**
     * 记录当前自定义Btn是否按下
     */

    /**
     * 长按的回调接口
     */
    private LongTouchListener mListener;

    /**
     * 按钮长按时 间隔多少毫秒来处理 回调方法
     */
    private int mtime;

    private Paint mBackgroundPaint;
    private Paint mButtonPaint;
    private Paint mBackgroundLinePaint;

    private int backgroundCircleLineWidth = 2;

    private float mButtonPointX, mButtonPointY;
    private float mBackgroundCircleLinePointX, mBackgroundCircleLinePointY;
    private float mBackgroundCircleLineRadius, mButtonDefaultRadius = 60, mButtonPressedRadius = 80, mButtonTempRadius = 0;

    private double currentButtonSwapAngale = 0;//中心按钮当前扫过的角度，以右边水平为x轴，0度
    private double currentBtnPointRadius = 0;//中心按钮中心点当前距离大圆中心点的距离
    private boolean pressDown;

    public SuperDiverButton(Context context) {
        super(context);
        init(context);
    }

    public SuperDiverButton(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SuperDiverButton(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        mBackgroundPaint = new Paint();
        mBackgroundPaint.setAntiAlias(true);
        mBackgroundPaint.setColor(Color.GRAY);
        mBackgroundPaint.setStyle(Paint.Style.STROKE);
        mBackgroundPaint.setStrokeWidth(backgroundCircleLineWidth);

        mButtonPaint = new Paint();
        mButtonPaint.setColor(Color.parseColor("#FFC107"));
        mButtonPaint.setStyle(Paint.Style.FILL_AND_STROKE);
//        mButtonPaint.setStrokeWidth(5);
        mButtonPaint.setAntiAlias(true);

        mBackgroundLinePaint = new Paint();
        mBackgroundLinePaint.setAntiAlias(true);
        mBackgroundLinePaint.setColor(Color.DKGRAY);
        mBackgroundLinePaint.setStyle(Paint.Style.STROKE);
        mBackgroundLinePaint.setStrokeWidth(20);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        hight = getMeasuredWidth();
        mBackgroundCircleLinePointX = width / 2f;
        mBackgroundCircleLinePointY = width / 2f;
        mBackgroundCircleLineRadius = width / 2f - backgroundCircleLineWidth / 2f - 20;
        mButtonPointX = width / 2f;
        mButtonPointY = hight / 2f;
    }

    private int width;
    private int hight;
    private double tempButtonCenterPointX = 0;
    private double tempButtonCenterPointY = 0;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(mBackgroundCircleLinePointX, mBackgroundCircleLinePointY, mBackgroundCircleLineRadius, mBackgroundPaint);


        double param = (mButtonPointY - mBackgroundCircleLinePointY) / (mButtonPointX - mBackgroundCircleLinePointX);
//        if (param < -1.0)
//
//            param = -1.0;
//
//        else if (param > 1.0)
//
//            param = 1.0;

        currentButtonSwapAngale = Math.toDegrees(Math.atan(Math.abs(param)));

        if (tempButtonCenterPointX > 0 && tempButtonCenterPointY < 0) {
            // TODO: 2019/3/27 第一象限
            Log.i(TAG, "第一象限：tempA:" + currentButtonSwapAngale);
        } else if (tempButtonCenterPointX <= 0 && tempButtonCenterPointY < 0) {
            // TODO: 2019/3/27 第二象限
            currentButtonSwapAngale = 180 - currentButtonSwapAngale;
            Log.i(TAG, "第二象限：tempA:" + currentButtonSwapAngale);
        } else if (tempButtonCenterPointX < 0 && tempButtonCenterPointY >= 0) {
            // TODO: 2019/3/27 第三象限
            currentButtonSwapAngale = 180 + currentButtonSwapAngale;
            Log.i(TAG, "第三象限：tempA:" + currentButtonSwapAngale);
        } else {
            // TODO: 2019/3/27 第四象限
            currentButtonSwapAngale = 360 - currentButtonSwapAngale;
            Log.i(TAG, "第四象限：tempA:" + currentButtonSwapAngale);
        }

        if (currentBtnPointRadius > mButtonDefaultRadius) {

            if (currentButtonSwapAngale > 45 && currentButtonSwapAngale <= 135) {
                // TODO: 2019/3/27 第一象限
                canvas.drawArc(10, 10, width - 10, width - 10, -45, -90, false, mBackgroundLinePaint);
                if (mListener != null) {
                    mListener.onTopTouch(SuperDiverButton.this, (int) currentBtnPointRadius, currentButtonSwapAngale);
                }
            } else if (currentButtonSwapAngale > 135 && currentButtonSwapAngale <= 225) {
                // TODO: 2019/3/27 第二象限
                canvas.drawArc(10, 10, width - 10, width - 10, -135, -90, false, mBackgroundLinePaint);
                if (mListener != null) {
                    mListener.onLeftTouch(SuperDiverButton.this, (int) currentBtnPointRadius, currentButtonSwapAngale);
                }
            } else if (currentButtonSwapAngale > 225 && currentButtonSwapAngale <= 315) {
                // TODO: 2019/3/27 第三象限
                canvas.drawArc(10, 10, width - 10, width - 10, 45, 90, false, mBackgroundLinePaint);
                if (mListener != null) {
                    mListener.onBottomTouch(SuperDiverButton.this, (int) currentBtnPointRadius, currentButtonSwapAngale);
                }
            } else if (currentButtonSwapAngale > 0 && (currentButtonSwapAngale > 315 || currentButtonSwapAngale <= 45)) {
                // TODO: 2019/3/27 第四象限
                canvas.drawArc(10, 10, width - 10, width - 10, -45, 90, false, mBackgroundLinePaint);
                if (mListener != null) {
                    mListener.onRightTouch(SuperDiverButton.this, (int) currentBtnPointRadius, currentButtonSwapAngale);
                }
            }
        }
//        if (pressDown) {
//            mButtonTempRadius = 50;
//        } else {
//            mButtonTempRadius = 0;
//        }
        canvas.drawCircle(mButtonPointX, mButtonPointY, mButtonDefaultRadius + mButtonTempRadius, mButtonPaint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mButtonTempRadius = mButtonPressedRadius - mButtonDefaultRadius;
                invalidate();
                return true;
            case MotionEvent.ACTION_MOVE:
                float tempButtonPointX = event.getX();
                float tempButtonPointY = event.getY();
                //相似三角形计算：x3=(r-中心点半径)*(y2-y1)/根号下((x2-x1)平方+(y2-y1)平方) 同理计算y3
                currentBtnPointRadius = Math.sqrt(Math.pow(tempButtonPointX - mBackgroundCircleLinePointX, 2) + Math.pow(tempButtonPointY - mBackgroundCircleLinePointY, 2));
//                double tempA = Math.toDegrees(Math.asin((tempButtonPointY - mBackgroundCircleLinePointY) / currentBtnPointRadius));

                if (currentBtnPointRadius < 30) {
                    return true;
                }

                if ((currentBtnPointRadius + mButtonDefaultRadius + mButtonTempRadius) < mBackgroundCircleLineRadius) {
                    mButtonPointX = tempButtonPointX;
                    mButtonPointY = tempButtonPointY;
                    tempButtonCenterPointX = mButtonPointX - mBackgroundCircleLinePointX;
                    tempButtonCenterPointY = mButtonPointY - mBackgroundCircleLinePointY;
                    invalidate();
                } else {
                    tempButtonCenterPointX = ((mBackgroundCircleLineRadius - mButtonDefaultRadius - mButtonTempRadius) * (tempButtonPointX - mBackgroundCircleLinePointX)) / currentBtnPointRadius;
                    tempButtonCenterPointY = ((mBackgroundCircleLineRadius - mButtonDefaultRadius - mButtonTempRadius) * (tempButtonPointY - mBackgroundCircleLinePointY)) / currentBtnPointRadius;

                    mButtonPointX = (float) (mBackgroundCircleLinePointX + tempButtonCenterPointX);
                    mButtonPointY = (float) (mBackgroundCircleLinePointY + tempButtonCenterPointY);
                    invalidate();
                }
                return true;
            case MotionEvent.ACTION_UP:
                mButtonTempRadius = 0;
                mButtonPointX = width / 2f;
                mButtonPointY = hight / 2f;
                invalidate();
                if (mListener != null) {
                    mListener.onStopTouch(SuperDiverButton.this);
                }
                return true;

            default:
                return super.onTouchEvent(event);
        }
    }

    /**
     * 给长按btn控件注册一个监听器。
     *
     * @param listener 监听器的实现。
     * @param time     多少毫秒时间间隔 来处理一次回调方法
     */
    public void setOnLongTouchListener(LongTouchListener listener, int time) {
        mListener = listener;
        mtime = time;
    }

    /**
     * 长按监听接口，使用按钮长按的地方应该注册此监听器来获取回调。
     */
    public interface LongTouchListener {

        /**
         * 处理长按的回调方法
         */
        void onLeftTouch(View view, int progress, double angle);

        void onTopTouch(View view, int progress, double angle);

        void onRightTouch(View view, int progress, double angle);

        void onBottomTouch(View view, int progress, double angle);

        void onStopTouch(View view);
    }
}
